<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('bar_members', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('bar_id')->comment('贴吧ID');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->enum('role', ['member', 'owner'])->default('member')->comment('角色：member普通成员，owner吧主');
            $table->timestamp('joined_at')->useCurrent()->comment('加入时间');
            $table->timestamps();

            $table->foreign('bar_id')->references('id')->on('bars')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->unique(['bar_id', 'user_id']); // 一个用户在一个贴吧只能有一条记录
            $table->index('bar_id');
            $table->index('user_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('bar_members');
    }
};
